#include <QtGui/QApplication>
#include <QMessageBox>
#include <QIcon>
#include <QTextCodec>
#include "qtimreceiverdlg.h"
#include "wwmaintabdialog.h"
#include "imlogindlg.h"

#include <QWebView>

#include "../Core/msgbus_interface.h"
#include "../Core/xparam.hpp"
#include "../Core/NetMsgBusHelper.hpp"
#include "../Biz/WWCommonUtility.h"
#include "../Core/condition.hpp"

#include "../Biz/ConfigurationMgr.hpp"
#include "../Core/MsgHandlerMgr.h"
#include "../Core/NetMsgBusFilterMgr.h"
#include "../Core/SimpleLogger.h"

#include <boost/noncopyable.hpp>
#include <string>
#include <stdio.h>

using namespace NetMsgBus;
using namespace wwcommon;
using namespace wwbiz;
using namespace core;

class Cleaner
{
public:
    ~Cleaner()
    {
        NetMsgBusDisConnect();
        DestroyMsgBus();
        threadpool::destroy_thread_pool();
        printf("QTIMReceiverMain Quit!\n");
    }
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QApplication::setWindowIcon(QApplication::windowIcon());
    QApplication::setWindowIcon(QIcon("./wangwang-icon.png"));
    QApplication::setApplicationName("阿里旺旺 for Mac/Linux");
    QTextCodec *utf8codec = QTextCodec::codecForName("UTF-8");
    QTextCodec::setCodecForCStrings(utf8codec);
    if(argc < 3)
        return -1;
    qRegisterMetaType<std::string>("std::string");
    qRegisterMetaType<NetMsgBus::MsgBusParam>("MsgBusParam");

    std::string wwmain_name("IMMainBiz");
    wwmain_name = argv[1];
    std::string receiver_name("IMReceiver");
    receiver_name = argv[2];
    //QMessageBox::information(NULL, "test", QString(QLatin1String(std::string(wwmain_name + receiver_name).c_str())));
    // read global configuration
    bool ret = ConfigurationMgr::Instance().LoadConfig();
    if(!ret)
    {
        printf("read configuration error, CmdIMReceiverMain start failed.\n");
        return -1;
    }

    int server_port;
    int imreceiver_port;
    IMGet_CommonConfig("serverport", server_port);
    IMGet_CommonConfig("imreceiverport", imreceiver_port);

    Cleaner cleaner_;

    threadpool::init_thread_pool();

    // init the logger.
    int loglevel = lv_warn;
    IMGet_CommonConfig("loglevel", loglevel);
    SimpleLogger::Instance().Init(utility::GetModulePath() + "/Qtlog.log", (LogLevel)loglevel);

    InitMsgBus(0);
    int retry = 3;
    while(0 != NetMsgBusConnectServer("127.0.0.1",server_port))
    {
        if(retry-- < 0)
        {
            printf("connect msgbus server error.\n");
            DestroyMsgBus();
            return -1;
        }
        sleep(1);
    }
    RegisterReceiverHandlerPtr receiver_handler;
    MsgHandlerMgr::CreateInstance(receiver_handler);

    unsigned short result_port = (unsigned short)imreceiver_port;
    if( 0 != NetMsgBusRegReceiver(receiver_name, "", result_port))
    {
        printf("register receiver client error.\n");
        return -1;
    }
    // make sure register is success return
    if(!receiver_handler->WaitForRsp())
    {
        printf("reg receiver failed, quit the receiver.\n");
        return -1;
    }

    FilterMgr::AddIncludeSender(wwmain_name);

    int result = 0;
    {
        boost::shared_ptr<WWMainTabDialog> maintab(new WWMainTabDialog());
        //boost::shared_ptr<QTIMReceiverDlg> receiver(new QTIMReceiverDlg());
        maintab->InitMsgHandler();

        if(argc > 3)
        {
            maintab->m_logindlg->setLoginName(argv[3]);
        }

        maintab->SetWWMainUniqueFlag(wwmain_name);
        maintab->m_im_mgr->NotifyReadyToWWMain();

        if(QDialog::Accepted == maintab->m_logindlg->exec())
        {
            maintab->show();
            maintab->m_im_mgr->show();
            result = a.exec();

            maintab->m_im_mgr->NotifyQuitToWWMain();
            maintab->m_im_mgr->RemoveAllHandlers();
        }
    }
    receiver_handler->RemoveAllHandlers();
    sleep(1);

    return result;
}
